<?php
      include_once('../lang/lang.php');
      include_once('../lang/'.$lang_file);
      
      $is_ok = false;
      if( isSet($_POST['host']) &&
            isSet($_POST['user']) &&
            isSet($_POST['db']) &&
            isSet($_POST['prefix']))
      {

            $host = $_POST['host'];
            $user = $_POST['user'];
            $pass = $_POST['pass'];
            $prefix = $_POST['prefix'];
            $db = $_POST['db'];
            
            $table_name = $prefix.'_ADMINS';
            
            $link = mysqli_connect($host, $user, $pass) or die($lang["FAILED_TO_CONNECT"]);
            mysqli_select_db($link, $db) or die($lang["FAILED_TO_SELECT_DB"].': '.mysqli_error($link));
            $query = 'CREATE TABLE '.$table_name.'(
                  id          int not null auto_increment,
                  name        varchar(40) not null,
                  pass        varchar(40) not null,
                  
                  add_admins  bit not null,
                  edit_admins bit not null,
                  del_admins  bit not null,
                  
                  add_vips    bit not null,
                  edit_vips   bit not null,
                  del_vips    bit not null,
                  
                  add_vips_t  bit not null,
                  edit_vips_t bit not null,
                  del_vips_t  bit not null,
                  
                  add_srv  bit not null,
                  edit_srv bit not null,
                  del_srv  bit not null,

                  constraint '.$table_name.'_PK primary key (id),
                  constraint '.$table_name.'_UK unique key (name))';

            if(!mysqli_query($link,  $query)){
                  $error = mysqli_error($link);
                  mysqli_close($link);
                  echo $query;
                  echo '<br/>';
                  die($error);
            }
            echo $lang['TABLE'].' '.$table_name.' '.$lang['SUCCESFULY_CREATED'].'<br/>';
            
            $admin_name = $_POST['admin_name'];
            $admin_name = mysql_real_escape_string($admin_name);
            $admin_pass = $_POST['admin_pass'];
            $admin_pass = mysql_real_escape_string($admin_pass);
            
            $query = 'INSERT INTO '.$table_name.' VALUES(null, \''.$admin_name.'\', \''.$admin_pass.'\',
            1,1,1,
            1,1,1,
            1,1,1,
            1,1,1)';
            if(!mysqli_query($link,  $query)){
                  $error = mysqli_error($link);
                  mysqli_close($link);
                  echo $query;
                  echo '<br/>';
                  die($error);
            }
            echo $lang['ADMIN_ADDED'].'<br/>';

            $table_name = $prefix.'_SRV';
            $query = 'CREATE TABLE '.$table_name.' (
                   id         int not null auto_increment,
                   name       varchar(40) not null,
                   ip         varchar(30) not null,                   
                   constraint '.$table_name.'_PK primary key (id),
                   constraint '.$table_name.'_UK unique key (name),
                   constraint '.$table_name.'_UK2 unique key (ip))';
                   
            if(!mysqli_query($link,  $query)){
                  $error = mysqli_error($link);
                  mysqli_close($link);
                  echo $query;
                  echo '<br/>';
                  die($error);
            }
            echo $lang['TABLE'].' '.$table_name.' '.$lang['SUCCESFULY_CREATED'].'<br/>';

            $table_name = $prefix.'_VIP_TMPL';
            $vip_tmpl = $prefix.'_VIP_TMPL';
            
            
            $query = 'CREATE TABLE '.$table_name.' (
                   id         int not null auto_increment,
                   name       varchar(40) not null,
                   long_jump  bit not null,
                   double_jump bit not null,
                   see_damage bit not null,
                   ban        bit not null,
                   kick       bit not null,
                   ss         bit not null,
                   constraint '.$table_name.'_PK primary key (id),
                   constraint '.$table_name.'_UK unique key (name))';
                   
             if(!mysqli_query($link,  $query)){
                  $error = mysqli_error($link);
                  mysqli_close($link);
                  echo $query;
                  echo '<br/>';
                  die($error);
             }
             echo $lang['TABLE'].' '.$table_name.' '.$lang['SUCCESFULY_CREATED'].'<br/>';
             
             $table_name = $prefix.'_VIP_TMPL_AHC';
             $query = 'CREATE TABLE '.$table_name.' (
                   id         int not null auto_increment,
                   vip        int not null,
                   armour     int not null,
                   hp_max     int not null,
                   hp_kill    int not null,
                   hp_hs      int not null,
                   cash_round int not null,
                   cash_kill  int not null,
                   cash_hs    int not null,
                   constraint '.$table_name.'_PK primary key (id),
                   constraint '.$table_name.'_UK unique key (vip),
                   constraint '.$table_name.'_FK foreign key (vip) references '.$vip_tmpl.'(id) on delete cascade)';

             if(!mysqli_query($link,  $query)){
                  $error = mysqli_error($link);
                  mysqli_close($link);
                  echo $query;
                  echo '<br/>';
                  die($error);
             }
             echo $lang['TABLE'].' '.$table_name.' '.$lang['SUCCESFULY_CREATED'].'<br/>';

             $table_name = $prefix.'_VIP_TMPL_WPN';
             $query = 'CREATE TABLE '.$table_name.' (
                  id          int not null auto_increment,
                  vip         int not null,
                  ammo        bit not null,
                  deagle_round int not null,
                  he          int not null,
                  he_round    int not null,
                  fb          int not null,
                  fb_round    int not null,
                  sg          int not null,
                  sg_round    int not null,
                  constraint '.$table_name.'_PK primary key (id),
                  constraint '.$table_name.'_UK unique key (vip),
                  constraint '.$table_name.'_FK foreign key (vip) references '.$vip_tmpl.'(id) on delete cascade)';

             if(!mysqli_query($link,  $query)){
                  $error = mysqli_error($link);
                  mysqli_close($link);
                  echo $query;
                  echo '<br/>';
                  die($error);
             }
             echo $lang['TABLE'].' '.$table_name.' '.$lang['SUCCESFULY_CREATED'].'<br/>';
             
             $table_name = $prefix.'_VIP_TMPL_WPN_PRIM';
             $query = 'CREATE TABLE '.$table_name.' (
                  id          int not null auto_increment,
                  vip         int not null,
                  mac10_round int not null,
                  aug_round   int not null,
                  ump45_round int not null,
                  sg550_round int not null,
                  galil_round int not null,
                  famas_round int not null,
                  awp_round   int not null,
                  mp5navy_round int not null,
                  m249_round  int not null,
                  m3_round    int not null,
                  m4a1_round  int not null,
                  tmp_round   int not null,
                  g3sg1_round int not null,
                  sg552_round int not null,
                  ak47_round  int not null,
                  p90_round   int not null,
                   
                  constraint '.$table_name.'_PK primary key (id),
                  constraint '.$table_name.'_UK unique key (vip),
                  constraint '.$table_name.'_FK foreign key (vip) references '.$vip_tmpl.'(id) on delete cascade)';

             if(!mysqli_query($link,  $query)){
                  $error = mysqli_error($link);
                  mysqli_close($link);
                  echo $query;
                  echo '<br/>';
                  die($error);
             }
             echo $lang['TABLE'].' '.$table_name.' '.$lang['SUCCESFULY_CREATED'].'<br/>';

             $table_name = $prefix.'_VIPS';
             $query = 'CREATE TABLE '.$table_name.' (
                  id          int not null auto_increment,
                  name        varchar(40) not null,
                  pass        varchar(40) not null,
                  constraint '.$table_name.'_PK primary key (id),
                  constraint '.$table_name.'_UK unique key (name))';
            if(!mysqli_query($link,  $query)){
                  $error = mysqli_error($link);
                  mysqli_close($link);
                  echo $query;
                  echo '<br/>';
                  die($error);
            }
            echo $lang['TABLE'].' '.$table_name.' '.$lang['SUCCESFULY_CREATED'].'<br/>';
            
            $table_name = $prefix.'_VIPS_SRV';
            $query = 'CREATE TABLE '.$table_name.' (
                  id          int not null auto_increment,
                  vip         int not null,
                  start_date  datetime not null,
                  end_date    datetime not null,
                  server      int not null,
                  vip_tmpl    int not null,
                  constraint '.$table_name.'_PK primary key (id),
                  constraint '.$table_name.'_UK unique key (vip, server),
                  constraint '.$table_name.'_FK foreign key (vip) references '.$prefix.'_VIPS(id) on delete cascade,
                  constraint '.$table_name.'_FK2 foreign key (server) references '.$prefix.'_SRV(id),
                  constraint '.$table_name.'_FK3 foreign key (vip_tmpl) references '.$prefix.'_VIP_TMPL(id))';
            if(!mysqli_query($link,  $query)){
                  $error = mysqli_error($link);
                  mysqli_close($link);
                  echo $query;
                  echo '<br/>';
                  die($error);
            }
            echo $lang['TABLE'].' '.$table_name.' '.$lang['SUCCESFULY_CREATED'].'<br/>';
            
            $file = '../include/db_config.php';
            $content = '
<?php
      $host = "'.$host.'";
      $user = "'.$user.'";
      $pass = "'.$pass.'";
      $prefix = "'.$prefix.'";
      $db = "'.$db.'";
?'.'>';
            file_put_contents($file, $content);
            
            $table_name = $prefix.'_ADMINS';
            $query = 'SELECT * FROM '.$table_name.' WHERE name = \''.$admin_name.'\' AND PASS =\''.$admin_pass.'\'';
		$result = mysqli_query($link,  $query);
		if(!$result){
			$error = mysqli_error($link);
			mysqli_close($link);
			echo $query;
			echo '<br/>';
			die($error);
        }
		if($admin = mysqli_fetch_array($result)){
      		$_SESSION['loggedin'] = 'Y';
			$_SESSION['id'] = $admin['id'];
			$_SESSION['name'] = $admin['name'];
			$_SESSION['add_admins'] = $admin['add_admins'];
			$_SESSION['edit_admins'] = $admin['edit_admins'];
			$_SESSION['del_admins'] = $admin['del_admins'];
			$_SESSION['add_vips'] = $admin['add_vips'];
			$_SESSION['edit_vips'] = $admin['edit_vips'];
			$_SESSION['del_vips'] = $admin['del_vips'];
			$_SESSION['add_vips_t'] = $admin['add_vips_t'];
			$_SESSION['edit_vips_t'] = $admin['edit_vips_t'];
			$_SESSION['del_vips_t'] = $admin['del_vips_t'];
            $_SESSION['add_srv'] = $admin['add_srv'];
			$_SESSION['edit_srv'] = $admin['edit_srv'];
			$_SESSION['del_srv'] = $admin['del_srv'];
			header('Location: ../main.php');
		}
            
      } else{
            header('Location: ../index.php');
      }
?>
